/*
  Diamond Girl - Game where player collects diamonds.
  Copyright (C) 2005-2014  Joni Yrjänä <joniyrjana@gmail.com>
  
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


  Complete license can be found in the LICENSE file.
*/

#include "diamond_girl.h"
#include "font.h"
#include "cave.h"
#include "widget.h"
#include "highscore.h"
#include "title.h"
#include "sfx.h"
#include "map.h"
#include "playback.h"
#include "gfx.h"
#include <assert.h>

static struct widget * window;

static void on_replay_clicked(struct widget * this, enum WIDGET_BUTTON button);
static void on_window_unload(struct widget * this);
static void on_window_close_clicked(struct widget * this, enum WIDGET_BUTTON button);
static void on_window_exit(bool pressed, SDL_Event * event);

struct widget * widget_new_highscore_einfo(struct widget * parent, struct highscore_entry * entry)
{
  int x[2], y;
  
  window = widget_new_window(parent, 400, 280, NULL);
  assert(window != NULL);

  x[0] = 10;
  x[1] = x[0] + widget_width(window) / 2;
  y = 20;

  struct tm * tm;
  int deaths;
  int diamonds;
  int start_level;
  int levels_completed;
  struct playback * playback;
  char tbuf[64];
  int col, labelwidth;

  labelwidth = 110;

  deaths           = 0;
  diamonds         = entry->diamonds_collected;
  start_level      = 0;
  levels_completed = 0;
  playback         = highscore_playback(entry, globals.title_game_mode);

  if(playback != NULL)
    {
      deaths           = playback_get_girls_died(playback);
      start_level      = playback->level;
      levels_completed = playback_get_levels_completed(playback);
    }

  col = 0;
  tm = localtime(&entry->timestamp);
  widget_new_text(window, x[col], y, "Played on:");
  strftime(tbuf, sizeof tbuf, "%Y-%m-%d %H:%M", tm);
  widget_new_text(window, x[col]  + labelwidth, y, tbuf);

  y += font_height();

  col = 0;
  widget_new_text(window, x[col], y, "Cave:");
  snprintf(tbuf, sizeof tbuf, "%s", cave_displayname(entry->cave));
  widget_new_text(window, x[col] + labelwidth, y, tbuf);

  y += font_height();

  col = 0;
  widget_new_text(window, x[col], y, "Mode:");
  switch(globals.title_game_mode)
    {
    case GAME_MODE_CLASSIC:      strcpy(tbuf, "Classic");      break;
    case GAME_MODE_ADVENTURE:    strcpy(tbuf, "Adventure");    break;
    case GAME_MODE_PYJAMA_PARTY: strcpy(tbuf, "Pyjama Party"); break;
    }
  widget_new_text(window, x[col] + labelwidth, y, tbuf);

  y += font_height();

  if(globals.title_game_mode != GAME_MODE_PYJAMA_PARTY)
    {
      col = 0;
      widget_new_text(window, x[col], y, "Filename:");
      if(playback != NULL)
        snprintf(tbuf, sizeof tbuf, "highscores%s-%d", cave_filename(playback->cave), entry->playback_id);
      else
        strcpy(tbuf, "N/A");
      widget_new_text(window, x[col] + labelwidth, y, tbuf);
  
      y += font_height();
    }

  y += font_height() / 2;

  col = 0;
  widget_new_text(window, x[col], y, "Score:");
  snprintf(tbuf, sizeof tbuf, "%6d", entry->score);
  widget_new_text(window, x[col] + labelwidth, y, tbuf);

  col = 1;
  widget_new_text(window, x[col], y, "Start level:");
  if(start_level > 0)
    snprintf(tbuf, sizeof tbuf, "%2d", start_level);
  else
    snprintf(tbuf, sizeof tbuf, "%2s", "N/A");
  widget_new_text(window, x[col] + labelwidth, y, tbuf);

  y += font_height();

  col = 0;
  widget_new_text(window, x[col], y, "Diamonds:");
  snprintf(tbuf, sizeof tbuf, "%6d", diamonds); /* For pre-v3 highscorelists this shows 0 because there's no data. */
  widget_new_text(window, x[col] + labelwidth, y, tbuf);

  col = 1;
  widget_new_text(window, x[col], y, "End level:");
  snprintf(tbuf, sizeof tbuf, "%2d", entry->level); 
  widget_new_text(window, x[col] + labelwidth, y, tbuf);

  y += font_height();

  col = 0;
  if(globals.title_game_mode == GAME_MODE_PYJAMA_PARTY)
    {
      widget_new_text(window, x[col], y, "Girls:");
      snprintf(tbuf, sizeof tbuf, "%6d", entry->starting_girls);
      widget_new_text(window, x[col] + labelwidth, y, tbuf);
    }
  else
    {
      widget_new_text(window, x[col], y, "Deaths:");
      if(deaths > 0)
        snprintf(tbuf, sizeof tbuf, "%6d", deaths);
      else
        snprintf(tbuf, sizeof tbuf, "%6s", "N/A");
      widget_new_text(window, x[col] + labelwidth, y, tbuf);
    }

  col = 1;
  widget_new_text(window, x[col], y, "Completed:");
  if(start_level > 0)
    snprintf(tbuf, sizeof tbuf, "%2d", levels_completed);
  else
    snprintf(tbuf, sizeof tbuf, "%2s", "N/A");
  widget_new_text(window, x[col] + labelwidth, y, tbuf);

  y += font_height();

  if(entry->diamond_score > 0)
    {
      col = 0;
      widget_new_text(window, x[col], y, "Diam.score:");
      snprintf(tbuf, sizeof tbuf, "%6d", entry->diamond_score);
      widget_new_text(window, x[col] + labelwidth, y, tbuf);

      y += font_height();
    }

  if(entry->traits)
    {
      col = 0;
      widget_new_text(window, x[col], y, "Traits:");

      //obj = widget_new_trait_list(x[col] + 10, y, widget_width(window) - (x[col] + 10), entry->traits, entry->traits);
      int xpos, size, padding;
      
      xpos = 0;
      size = 24;
      padding = 2;
      for(trait_t trait = 0; trait < TRAIT_SIZEOF_; trait++)
        if(entry->traits & (1 << trait))
          {
            struct widget * obj;
            char * name;

            obj = widget_new_trait_button(window, x[col] + labelwidth + xpos * (size + padding), y, size, size, (1<<trait), true, false);
            name = trait_get_name(1<<trait);
            if(name != NULL)
              widget_set_tooltip(obj, name);

            xpos++;
            if(xpos >= (250 / (size + padding)))
              {
                xpos = 0;
                y += size + padding;
              }
          }
      
      if(xpos > 0)
        y += size + padding;
    }

  if(strlen(entry->notes) > 0)
    {
      col = 0;
      widget_new_text(window, x[col], y, "Notes:");
      y += font_height();
      widget_new_text(window, x[col] + 10, y, entry->notes);
      y += font_height();
    }

  y += font_height();

  struct widget * breplay, * bclose;

  breplay = NULL;
  if(globals.title_game_mode != GAME_MODE_PYJAMA_PARTY)
    {
      breplay = widget_new_button(window, 0, y, "Replay");
      widget_set_width(breplay, 100);
      widget_set_x(breplay, (widget_width(window) - widget_width(breplay)) / 2);
      widget_set_on_release(breplay, on_replay_clicked);
      widget_set_pointer(breplay, "highscore_entry", entry);
      if(playback == NULL)
        widget_set_enabled(breplay, false);
      y += widget_height(breplay) + 4;
    }

  bclose = widget_new_button(window, 0, y, "Close");
  widget_set_width(bclose, 100);
  widget_set_x(bclose, (widget_width(window) - widget_width(bclose)) / 2);
  widget_set_on_release(bclose, on_window_close_clicked);
  if(breplay != NULL)
    widget_set_navigation_updown(breplay, bclose);
  y += widget_height(bclose);
  
  widget_set_pointer(window, "previously_selected_object", widget_focus());
  widget_set_focus(bclose);

  widget_set_height(window, y + 10);


  /* This is a modal window. */
  widget_set_modal(window);
  /* Replace cancel and exit handlers. */
  ui_push_handlers();
  ui_set_handler(UIC_EXIT,   on_window_exit);
  ui_set_handler(UIC_CANCEL, on_window_exit);
  widget_set_on_unload(window, on_window_unload);

  return window;
}


static void on_window_unload(struct widget * this DG_UNUSED)
{
  ui_pop_handlers();
}

static void on_window_exit(bool pressed, SDL_Event * event DG_UNUSED)
{
  if(pressed == true)
    {
      struct widget * w;

      w = widget_get_pointer(window, "previously_selected_object");

      widget_delete(window);

      if(w != NULL)
        widget_set_focus(w);
    }
}

static void on_window_close_clicked(struct widget * this DG_UNUSED, enum WIDGET_BUTTON button DG_UNUSED)
{
  on_window_exit(true, NULL);
}




static void on_replay_clicked(struct widget * this, enum WIDGET_BUTTON button DG_UNUSED)
{
  struct highscore_entry * entry;
  struct playback * playback;

  entry = widget_get_pointer(this, "highscore_entry");
  assert(entry != NULL);

  playback = highscore_playback(entry, globals.title_game_mode);
  if(playback != NULL)
    {
      char * pberr;

      pberr = playback_check(playback);
      if(pberr == NULL)
        {
          char playback_title[64];
          struct cave * cave;
          char * cave_fn;
          
          cave = cave_get(globals.title_game_mode, entry->cave);
          cave_fn = cave_filename(entry->cave);
          while(*cave_fn == '-') /* Skip leading "-" of the cave filename. */
            cave_fn++;
          if(strlen(cave_fn) > 0)
            snprintf(playback_title, sizeof playback_title, "%s-", cave_fn);
          else
            strcpy(playback_title, "");
          snprintf(playback_title + strlen(playback_title), sizeof playback_title - strlen(playback_title), "%d", entry->playback_id);
          title_game(cave, playback, playback_title);
          
          sfx_volume(0.05f);
          sfx_music(MUSIC_TITLE, 1);
          ui_set_last_user_action(time(NULL));
        }
      else
        {
          widget_new_message_window("ERROR", pberr);
          free(pberr);
        }
    }
}
